<?php
namespace app\controllers;


use Yii;
use yii\base\ExitException;
use yii\rest\ActiveController;
use yii\filters\VerbFilter;

class BaseController extends ActiveController{
    public $modelClass = '';
    /**
     * 格式如下
     * ["searchHouse"=>["name"=>"app\com\simtoon\models\SearchHouse", "serializer"=>"实现SerializerJson类"]]
     *
     * @var array
     */
    private $modelMap = array("SysUser"=>array("name"=>"app\com\simtoon\app\SysUser", "serializer"=>"app\controllers\SerializerToJSon"),//蜂狐app用户相关接口
                            "SendSMS"=>array("name"=>"app\com\simtoon\app\SendSMS", "serializer"=>"app\controllers\SerializerToJSon"),
                            "SysInfo"=>array("name"=>"app\com\simtoon\app\SysInfo", "serializer"=>"app\controllers\SerializerToJSon"),
                            "Share"=>array("name"=>"app\com\simtoon\app\Share", "serializer"=>"app\controllers\SerializerToJSon"),
                            "SysFunc"=>array("name"=>"app\com\simtoon\app\SysFunc", "serializer"=>"app\controllers\SerializerToJSon"),
                            "GroceryInfo"=>array("name"=>"app\com\simtoon\app\GroceryInfo", "serializer"=>"app\controllers\SerializerToJSon"),
                            "Groceryhandle"=>array("name"=>"app\com\simtoon\app\Groceryhandle", "serializer"=>"app\controllers\SerializerToJSon"),
                            "Sendgood"=>array("name"=>"app\com\simtoon\app\Sendgood", "serializer"=>"app\controllers\SerializerToJSon"),
                            "SendInstall"=>array("name"=>"app\com\simtoon\app\SendInstall", "serializer"=>"app\controllers\SerializerToJSon"),
                            "dayByday"=>array("name"=>"app\com\simtoon\app\dayByday", "serializer"=>"app\controllers\SerializerToJSon"),
                            );

    public $model;
    public $serializer;

    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'index'  => ['get','post']
                ],
            ],
        ];
    }

    public function actions(){

    }


    public function actionIndex(){

        try{
            //获得，
            $request = Yii::$app->request;
            $modl = $request->get('model');
            $meth = $request->get('method');
            $parm = $request->post();
            $this->model = $this->modelMap[$modl]['name'];
            $this->serializer = $this->modelMap[$modl]['serializer'];
            //调用
            $model = new $this->model;
            $post = array("isCallBeforeOutPut"=>true,"method"=>$meth,$parm);
            return $model->findDataToArray($post);
        }catch (Exception $e){
            LogUtils::error($e->getMessage(), __METHOD__);
            return array();
        }


    }


    /**
     * 用来在返回相应数据之前，调用数据处理
     *
     * @param \yii\base\Action $action
     * @param mixed $result
     */
    public function afterAction($action,$result){
        try{
            return Yii::createObject($this->serializer)->parseJson($result);
        }catch (Exception $e){
            LogUtils::error($e->getMessage(), __METHOD__);
            return array();
        }
    }

}

?>